import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Human {

	private final String PICTURE_HUMAN = "Human.png",
			PICTURE_HUMAN_MINI = "HumanMini.png";
	private final int VELOCITY = 2;

	private Image image, miniImage;

	private int x, y; // Position
	private int dx, dy; // Movement increments
	private int width, height; // Image properties
	private boolean visible;

	public boolean incoming; // True indicates the set of Buggers paired with
								// this Human is being generated

	public Human(int xInitial, int yInitial) {
		x = xInitial;
		y = yInitial;

		visible = true;
		incoming = false;

		ImageIcon ii = new ImageIcon(this.getClass().getResource(PICTURE_HUMAN));
		image = ii.getImage();

		ii = new ImageIcon(this.getClass().getResource(PICTURE_HUMAN_MINI));
		miniImage = ii.getImage();

		width = image.getWidth(null);
		height = image.getHeight(null);
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Rectangle getBounds() {
		return new Rectangle(x + width/2, y + height/2, width - 20, height - 20);
	}

	public Image getImage() {
		return image;
	}

	public Image getMiniImage() {
		return miniImage;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public void move() {
		x += dx;
		y += dy;

		if (x < 1) {
			x = 1;
		} else if ((x + width) > EndersGame.WINDOW_WIDTH) {
			x = EndersGame.WINDOW_WIDTH - width;
		}

		if (y < 1) {
			y = 1;
		} else if ((y + height) > (int) (EndersGame.WINDOW_HEIGHT * EndersGame.BOARD_HEIGHT)) {
			y = (int) (EndersGame.WINDOW_HEIGHT * EndersGame.BOARD_HEIGHT)
					- height;
		}
	}

	public void keyPressed(KeyEvent e) {

		int key = e.getKeyCode();

		if (key == KeyEvent.VK_LEFT) {
			dx = -VELOCITY;
		}

		if (key == KeyEvent.VK_RIGHT) {
			dx = VELOCITY;
		}

		if (key == KeyEvent.VK_UP) {
			dy = -VELOCITY;
		}

		if (key == KeyEvent.VK_DOWN) {
			dy = VELOCITY;
		}

		if (key == KeyEvent.VK_SPACE) {
			Board.accelerating = true;
		}
	}

	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();

		if (key == KeyEvent.VK_LEFT) {
			dx = 0;
		}

		if (key == KeyEvent.VK_RIGHT) {
			dx = 0;
		}

		if (key == KeyEvent.VK_UP) {
			dy = 0;
		}

		if (key == KeyEvent.VK_DOWN) {
			dy = 0;
		}

		if (key == KeyEvent.VK_SPACE) {
			Board.accelerating = false;
		}

		// If btns 1-n are released, switch Human and ArrayList<Bugger> between
		// Board and MiniMap
		if (key == KeyEvent.VK_1) {
			EndersGame.moveInsides(EndersGame.minimaps.get(0));
			// EndersGame.switchInsides(EndersGame.minimaps.get(0));
		}

		if (key == KeyEvent.VK_2) {
			EndersGame.moveInsides(EndersGame.minimaps.get(1));
			// EndersGame.switchInsides(EndersGame.minimaps.get(1));
		}

		if (key == KeyEvent.VK_3) {
			EndersGame.moveInsides(EndersGame.minimaps.get(2));
			// EndersGame.switchInsides(EndersGame.minimaps.get(2));
		}
	}
}
